package com.huluobo.a2112mvvmdouyindemo;

import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;

import com.huluobo.a2112mvvmdouyindemo.adapter.CommonPagerAdapter;
import com.huluobo.a2112mvvmdouyindemo.databinding.ActivityMainBinding;
import com.huluobo.a2112mvvmdouyindemo.home.HomeFragment;
import com.huluobo.a2112mvvmdouyindemo.message.MessageFragment;
import com.huluobo.a2112mvvmdouyindemo.mine.MineFragment;
import com.huluobo.a2112mvvmdouyindemo.startLive.StartLiveActivity;
import com.huluobo.a2112mvvmdouyindemo.video.VideoFragment;
import com.huluobo.mvvm_base.view.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity<ActivityMainBinding> {
    //vp2的adapter
    private CommonPagerAdapter commonPagerAdapter;

    //fragment数据源
    private List<Fragment> fragmentList = new ArrayList<>();

    @Override
    protected void initData() {
        fragmentList.clear();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new VideoFragment());
        fragmentList.add(new MessageFragment());
        fragmentList.add(new MineFragment());

        commonPagerAdapter = new CommonPagerAdapter(this, fragmentList);
        dataBinding.mainVp2.setAdapter(commonPagerAdapter);
    }

    @Override
    protected void initView() {
        //声明动态权限
        requestPermissions(new String[]{
                RECORD_AUDIO,
                CAMERA,
                READ_EXTERNAL_STORAGE,
                WRITE_EXTERNAL_STORAGE
        }, 102);

        //vp2联动底部导航栏,当vp2翻动时,切换底部导航的下标
        dataBinding.mainVp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case 0:
                        dataBinding.mainBottomNav.getMenu().getItem(0).setChecked(true);
                        break;
                    case 1:
                        dataBinding.mainBottomNav.getMenu().getItem(1).setChecked(true);
                        break;
                    case 2:
                        dataBinding.mainBottomNav.getMenu().getItem(3).setChecked(true);
                        break;
                    case 3:
                        dataBinding.mainBottomNav.getMenu().getItem(4).setChecked(true);
                        break;
                }
            }
        });

        //底部导航切换时和vp2联动
        dataBinding.mainBottomNav.setOnItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.main_tab_home:
                    dataBinding.mainVp2.setCurrentItem(0);
                    break;
                case R.id.main_tab_video:
                    dataBinding.mainVp2.setCurrentItem(1);
                    break;
                case R.id.main_tab_start_live:
                    Intent intent = new Intent(this, StartLiveActivity.class);
                    startActivity(intent);
                    break;
                case R.id.main_tab_message:
                    dataBinding.mainVp2.setCurrentItem(2);
                    break;
                case R.id.main_tab_mine:
                    dataBinding.mainVp2.setCurrentItem(3);
                    break;
            }

            return true;
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        dataBinding.mainBottomNav.getMenu().getItem(1).setChecked(true);
        dataBinding.mainVp2.setCurrentItem(1);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }
}